<template>
  <div>
    <el-dialog
      :title="obj.isadd?'轮播图添加':'轮播图编辑'"
      :visible.sync="obj.istrue"
      width="50%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <!-- 图片上传 -->
        <el-form-item label="图片">
          <div class="wrap">
            <h2>+</h2>
            <input
              type="file"
              class="input"
              @change="fileimg"
              v-if="obj.istrue"
            />
            <img :src="imgUrl" alt="" v-if="imgUrl" />
          </div>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiao()">取 消</el-button>
        <el-button type="primary" @click="add" v-if="obj.isadd">添 加</el-button>
        <el-button type="primary" @click="bianji" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<script>
import { mapActions, mapGetters } from "vuex";
import {banneradd,bannerinfo,banneredit} from '../../../request/api'
import { successAlert, warningAlert } from "../../../util/alert";
export default {
  props: ["obj"],
  data() {
    return {
      dialogVisible: true,
      imgUrl: "",
      form: {
        title: "",
        img: "",
        status: 1,
      },
    };
  },
  methods: {
    ...mapActions({
      reqbannerlist: "banner/reqchangelist",
    }),
    //   点击弹窗x执行的回调函数
    handleClose() {
      this.quxiao();
    },
    // 改变图片路径
    fileimg(e) {
      // console.log(e.target.files[0]);
      let file = e.target.files[0];
      this.imgUrl = URL.createObjectURL(file);
      this.form.img=file
      // console.log(file);
    },
    // 清空
    empty() {
      this.form = {
        title: "",
        img: "",
        status: 1,
      };
      this.imgUrl = "";
    },
    // 点击取消
    quxiao() {
      this.obj.istrue = false;
      this.empty();
    },
    // 点击添加
    add(){
      banneradd(this.form).then(res=>{
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          // 刷新数据
          this.reqbannerlist()
          // 关闭弹框清空内容
          this.quxiao();
        } else {
          warningAlert(res.data.msg);
        }
      })
    },
    // 获取一条信息
    getinfo(id){
      bannerinfo({id:id}).then(res=>{
        this.form=res.data.list
        this.imgUrl=this.$pre+res.data.list.img
        this.form.id=id
      })
    },
    // 点击编辑
    bianji(){
      console.log(this.form);
      banneredit(this.form).then(res=>{
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          // 刷新数据
          this.reqbannerlist()
          // 关闭弹框清空内容
          this.quxiao();
        } else {
          warningAlert(res.data.msg);
        }
      })
    }
  },
  computed: {
    ...mapGetters({
      // bannerlist: "banner/reqchangelist",
    }),
  },
  mounted() {
    // this.reqbannerlist();
  },
};
</script>

<style lang="less" scoped>
.wrap {
  width: 200px;
  height: 200px;
  border: 1px dashed #ccc;
  text-align: center;
  position: relative;
}
.wrap h2 {
  font-size: 50px;
  line-height: 200px;
  margin: 0 auto;
}
.wrap img {
  position: absolute;
  top: 0;
  left: 0;
  width: 200px;
  height: 200px;
  z-index: 2;
}
.wrap .input {
  position: absolute;
  top: 0;
  left: 0;
  width: 200px;
  height: 200px;
  border: none;
  outline: none;
  opacity: 0;
  z-index: 3;
}
</style>